﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Calendar;
using SistemaPetShop.Negocio;

namespace SistemaPetShop.Forms
{
    public partial class cadAgenda : Form
    {
        List<Appointment> m_Appointments;

        StringBuilder str = new StringBuilder();

        Cliente cliente = new Cliente();
        Animal animal = new Animal();
        Agenda agenda = new Agenda();
        DataTable dt = new DataTable();
        BaseClass baseClass = new BaseClass();

        public cadAgenda()
        {
            InitializeComponent();
            m_Appointments = new List<Appointment>();
            
            DateTime m_Date = DateTime.Now;

            m_Date = m_Date.AddHours(10 - m_Date.Hour);
            m_Date = m_Date.AddMinutes(-m_Date.Minute);
            dayView1.StartDate = DateTime.Now;
            dayView1.NewAppointment += new NewAppointmentEventHandler(dayView1_NewAppointment);
            dayView1.SelectionChanged += new EventHandler(dayView1_SelectionChanged);
            dayView1.ResolveAppointments += new Calendar.ResolveAppointmentsEventHandler(this.dayView1_ResolveAppointments);

            dayView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.dayView1_MouseMove);


            _CarregaCalendario();
        }


        private void _CarregaComboCliente()
        {
            cboCliente.DataSource = cliente._ListaNomeCliente();
            cboCliente.DisplayMember = "nome";
            cboCliente.ValueMember = "idCliente";
            _CarregaComboAnimal();
        }

        private void _CarregaCalendario()
        {
            dt = agenda._ListaAgenda();

            m_Appointments.Clear();

            foreach (DataRow linhas in dt.Rows)
	        {
                Appointment m_App = new Appointment();
                m_App.StartDate = Convert.ToDateTime(linhas[0].ToString());
                m_App.EndDate = Convert.ToDateTime(linhas[1].ToString());
                m_App.BorderColor = Color.Red;
                m_Appointments.Add(m_App);
                dayView1.Invalidate();

                str.Append("Nome: ");
                str.Append(linhas[5].ToString());
                str.Append(" || ");
                str.Append("Apelido: ");
                str.Append(linhas[6].ToString());
                str.Append(" || ");
                str.Append("Procedimento: ");
                str.Append(linhas[2].ToString());
                str.Append(" || ");
                str.Append("Observação: ");
                str.Append(linhas[3].ToString());
                str.Append(" || ");
                str.Append("Valor: ");
                str.Append(baseClass._ApresentaValorMonetario(linhas[4].ToString()));
                m_App.Title = str.ToString().ToUpper();
                str.Remove(0, str.Length);
	        }



        }

        private void _CarregaComboAnimal()
        {


            if (cboCliente.SelectedValue.ToString() != "System.Data.DataRowView")
            {
                cboApelido.DataSource = animal._ListaNomeAnimal(Convert.ToInt32(cboCliente.SelectedValue.ToString()));
                cboApelido.DisplayMember = "apelido";
                cboApelido.ValueMember = "idAnimal";
            }
            

        }


        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // MessageBox.Show("teste");

        }

        private void cadAgenda_Load(object sender, EventArgs e)
        {
            _CarregaComboCliente();

        }

        private void dayView1_SelectionChanged(object sender, EventArgs e)
        {
            //label3.Text = dayView1.SelectionStart.ToString() + ":" + dayView1.SelectionEnd.ToString();
        }

        private void dayView1_NewAppointment(object sender, NewAppointmentEventArgs args)
        {
            Appointment m_Appointment = new Appointment();

            m_Appointment.StartDate = args.StartDate;
            m_Appointment.EndDate = args.EndDate;
            m_Appointment.Title = args.Title;

            m_Appointments.Add(m_Appointment);
        }

        private void dayView1_ResolveAppointments(object sender, ResolveAppointmentsEventArgs args)
        {
            List<Appointment> m_Apps = new List<Appointment>();

            foreach (Appointment m_App in m_Appointments)
                if ((m_App.StartDate >= args.StartDate) &&
                    (m_App.StartDate <= args.EndDate))
                    m_Apps.Add(m_App);

            args.Appointments = m_Apps;
        }

        private void dayView1_MouseMove(object sender, MouseEventArgs e)
        {
            //label2.Text = dayView1.GetTimeAt(e.X, e.Y).ToString();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            dayView1.StartDate = monthCalendar1.SelectionStart;
            _CarregaCalendario();
        }

        private void btnNovoApontamento_Click(object sender, EventArgs e)
        {
            try
            {
                Appointment m_App = new Appointment();
                m_App.StartDate = dayView1.SelectionStart;
                m_App.EndDate = dayView1.SelectionEnd;
                m_App.BorderColor = Color.Red;
                m_Appointments.Add(m_App);
                dayView1.Invalidate();
                str.Append("Nome: ");
                str.Append(cboCliente.Text);
                str.Append(" || ");
                str.Append("Apelido: ");
                str.Append(cboApelido.Text);
                str.Append(" || ");
                str.Append("Procedimento: ");
                str.Append(cboProcedimento.Text);
                str.Append(" || ");
                str.Append("Observação: ");
                str.Append(txtObservacao.Text);
                str.Append(" || ");
                str.Append("Valor: ");
                str.Append(txtValor.Text);
                m_App.Title = str.ToString().ToUpper();
                str.Remove(0, str.Length);

                DateTime data = dayView1.SelectionStart;

                bool ret = agenda._InsereNovaAgenda(dayView1.SelectionStart, cboProcedimento.Text, txtObservacao.Text, txtValor.Text, Convert.ToInt32(cboCliente.SelectedValue), Convert.ToInt32(cboApelido.SelectedValue),dayView1.SelectionEnd);

                if (ret == true)
                {
                    MessageBox.Show("Procedimento Agendado com sucesso", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao tentar adicionar registro na agenda, por favor entre em contato com o administrador do sistema " + ex.ToString(), "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
        
        
        }

        private void cboCliente_SelectedIndexChanged(object sender, EventArgs e)
        {
            _CarregaComboAnimal();
        }

        private void cboCliente_SelectedValueChanged(object sender, EventArgs e)
        {
            //_CarregaComboAnimal();
        }


        private void btnCancelar_Click(object sender, EventArgs e)
        {
            conAgendamento frmagendamento = new conAgendamento();
            frmagendamento.Show();
        }







    }
}
